package com.yy.wga.goods.service.manager;
import com.yy.wga.goods.feign.enums.EPromoter;
import com.yy.wga.goods.feign.request.SharerProductSetCommissionReq;
import com.yy.wga.goods.feign.request.SharerSetCommissionReq;
import com.yy.wga.goods.feign.response.PromoterStatusResp;
import com.yy.wga.goods.service.api.response.bind.GetPromoterRegisterAndBindStatusResp;
import com.yy.wga.goods.service.api.response.commission.SharerProductCommissionResp;
import com.yy.wga.goods.service.service.PromoterService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Slf4j
@Component
public class PromoterManager {

    @Resource
    private PromoterService promoterService;


    public PromoterStatusResp getPromoterStatus(String openId) {
        PromoterStatusResp resp = new PromoterStatusResp();
        GetPromoterRegisterAndBindStatusResp statusResp = promoterService.getPromoterRegisterAndBindStatus(openId);
        Integer registerStatus = statusResp.getRegisterStatus();

        // 处理未注册情况
        if (EPromoter.RegisterStatus.needInvokeRegisterProcess(registerStatus)) {
            setUnregisterResponse(resp, statusResp);
            return resp;
        }
        // 已注册，处理绑定状态
        if (!EPromoter.RegisterStatus.isRegister(registerStatus)) {
            setOtherResponse(resp);
            return resp;
        }
        handleBindStatus(resp, statusResp);
        return resp;
    }

    public void setSharerCommission(SharerSetCommissionReq req) {
        promoterService.setSharerCommission(req);
    }

    public void setSharerProductCommission(SharerProductSetCommissionReq req) {
        promoterService.setSharerProductCommission(req);

    }

    public Integer getSharerProductCommission(String spuId) {
        SharerProductCommissionResp sharerProductCommission = promoterService.getSharerProductCommission(spuId);
        if (sharerProductCommission.isSet()) {
            return sharerProductCommission.getCommissionRatio();
        }
        return 0;
    }


    private void setUnregisterResponse(PromoterStatusResp resp, GetPromoterRegisterAndBindStatusResp statusResp) {
        resp.setBusinessType(statusResp.getRegisterBusinessType());
        resp.setQueryString(statusResp.getRegisterQueryString());
        resp.setType(EPromoter.PromoterStatus.UN_REGISTER.getType());
        resp.setMessage(EPromoter.PromoterStatus.UN_REGISTER.getMessage());
    }

    private void setOtherResponse(PromoterStatusResp resp) {
        resp.setType(EPromoter.PromoterStatus.OTHER.getType());
        resp.setMessage(EPromoter.RegisterStatus.NEED_REAL_NAME.getMessage());
    }

    private void handleBindStatus(PromoterStatusResp resp, GetPromoterRegisterAndBindStatusResp statusResp) {
        Integer bindStatus = statusResp.getBindStatus();
        if (EPromoter.BindStatus.isBind(bindStatus)) {
            resp.setType(EPromoter.PromoterStatus.REGISTER_AND_BIND.getType());
            resp.setMessage(EPromoter.PromoterStatus.REGISTER_AND_BIND.getMessage());
        } else {
            resp.setBusinessType(statusResp.getBindBusinessType());
            resp.setQueryString(statusResp.getBindQueryString());
            resp.setType(EPromoter.PromoterStatus.UN_BIND.getType());
            resp.setMessage(EPromoter.PromoterStatus.UN_BIND.getMessage());
        }
    }
}
